import { setStore, getStore, removeStore } from "@/libs/store";
import store from '@/store'


const TokenKey = 'TOKEN'

export function setToken(token) {
    setStore({ name: TokenKey, content: token, type: 'session' })
}

export function getToken() {
    return getStore({ name: TokenKey })
}

export function removeToken() {
    removeStore({ name: TokenKey })
}

/**
 * 按钮权限，判断用户是否有该按钮的权限，无权限则不显示按钮
 * @param auth
 * @returns {boolean}
 */
export function isAuth(auth) {
    if(store.getters.isAdmin) {
        return true
    } else {
        let authorize = store.getters.action_permissions[auth]
        if(authorize == undefined) {
            return false
        } else {
            return true
        }
    }
}

/**
 * 页面级验证权限，验证是否有权限访问该组件
 * @param auth 路由中的meta属性，meta: {auth: 'upms:role:ui'}
 * @returns {boolean}
 */
export function hasPagePermission(auth) {
    if(store.getters.isAdmin) {
        return true
    } else {
        //待验证权限不为空时才需要判断是否有权限
        if(auth) {
            let authorize = store.getters.page_permissions
            return authorize.some((currentValue,index,arr) => currentValue === auth)
        } else {
            //auth为undefined是不需要验证权限
            return true
        }
    }

}
